home *** CD-ROM | disk | FTP | other *** search
- package com.commerceone.util.urn;
-
- import java.io.Serializable;
-
- public class URN implements Serializable {
- private String nid;
- private String nss;
-
- public String getNSS() {
- return this.nss;
- }
-
- private static boolean isValidNSSCharStatic(char nssChar) {
- return nssChar == '%' || nssChar >= 'A' && nssChar <= 'Z' || nssChar >= 'a' && nssChar <= 'z' || nssChar >= '0' && nssChar <= '9' || nssChar >= '\'' && nssChar <= '.' || nssChar == '!' || nssChar == '$' || nssChar == ':' || nssChar == ';' || nssChar == '=' || nssChar == '@' || nssChar == '_';
- }
-
- public String toString() {
- return new String("urn:" + this.nid + ":" + this.nss);
- }
-
- public URN(String urnString) throws MalformedURNException {
- this.checkForValidURN(urnString);
- String testNID = this.extractNIDFromString(urnString);
- if (testNID == null) {
- throw new MalformedURNException("Bad or missing NID.");
- } else {
- String testNSS = this.extractNSSFromString(urnString);
- if (testNSS == null) {
- throw new MalformedURNException("Bad or missing NSS.");
- } else {
- this.nid = testNID;
- this.nss = testNSS;
- }
- }
- }
-
- public URN(String nidString, String nssString) throws MalformedURNException {
- if (!this.isValidNID(nidString)) {
- throw new MalformedURNException("Bad or missing NID.");
- } else if (!this.isValidNSS(nssString)) {
- throw new MalformedURNException("Bad or missing NSS.");
- } else {
- this.nid = nidString;
- this.nss = nssString;
- }
- }
-
- public int hashCode() {
- return this.nid.hashCode() * this.nss.hashCode();
- }
-
- public static String decodeString(String inString) {
- StringBuffer outStringBuffer = new StringBuffer(inString.length());
-
- char testChar;
- for(int pos = 0; pos < inString.length(); outStringBuffer.append(testChar)) {
- testChar = inString.charAt(pos++);
- if (testChar == '%') {
- testChar = (char)Integer.parseInt(inString.substring(pos, pos + 2), 16);
- pos += 2;
- }
- }
-
- return new String(outStringBuffer);
- }
-
- private String extractNIDFromString(String urnString) throws MalformedURNException {
- this.checkForValidURN(urnString);
- String nidAndNss = urnString.substring(4);
- int pos = nidAndNss.indexOf(58);
- if (pos == -1) {
- return null;
- } else {
- String nidString = nidAndNss.substring(0, pos);
- return this.isValidNID(nidString) ? nidString : null;
- }
- }
-
- private boolean isValidNIDChar(char nidChar) {
- return nidChar == '-' || nidChar >= 'A' && nidChar <= 'Z' || nidChar >= 'a' && nidChar <= 'z' || nidChar >= '0' && nidChar <= '9';
- }
-
- private boolean isValidNSS(String nssString) throws MalformedURNException {
- if (nssString == null) {
- return false;
- } else {
- int nssLength = nssString.length();
- if (nssLength < 1) {
- return false;
- } else {
- for(int pos = 0; pos < nssLength; ++pos) {
- char testChar = nssString.charAt(pos);
- if (!this.isValidNSSChar(testChar)) {
- return false;
- }
-
- if (testChar == '%') {
- if (pos + 2 >= nssLength || !this.isValidHexChar(nssString.charAt(pos + 1)) || !this.isValidHexChar(nssString.charAt(pos + 2))) {
- return false;
- }
- } else if (this.isReservedChar(testChar)) {
- throw new MalformedURNException("unescaped reserved character '" + testChar + "' used.");
- }
- }
-
- return true;
- }
- }
- }
-
- private boolean isReservedChar(char rawChar) {
- return rawChar == '/' || rawChar == '?' || rawChar == '#';
- }
-
- public String getNID() {
- return this.nid;
- }
-
- private void checkForValidURN(String urnString) throws MalformedURNException {
- if (urnString != null && urnString.length() >= 7) {
- if (!urnString.startsWith("urn:") && !urnString.startsWith("URN:")) {
- throw new MalformedURNException("Bad or missing URN prefix.");
- }
- } else {
- throw new MalformedURNException("Bad or missing URN.");
- }
- }
-
- public boolean equals(Object obj) {
- if (!(obj instanceof URN)) {
- return false;
- } else {
- String thisNID = this.nid.toLowerCase();
- String thatNID = ((URN)obj).getNID().toLowerCase();
- String thisNSS = this.nss.toLowerCase();
- String thatNSS = ((URN)obj).getNSS().toLowerCase();
- return thisNID.equals(thatNID) && thisNSS.equals(thatNSS);
- }
- }
-
- public boolean equalsString(String string) {
- try {
- URN newURN = new URN(string);
- return this.equals(newURN);
- } catch (MalformedURNException var3) {
- return false;
- }
- }
-
- private String extractNSSFromString(String urnString) throws MalformedURNException {
- this.checkForValidURN(urnString);
- String nidAndNss = urnString.substring(4);
- int pos = nidAndNss.indexOf(58);
- if (pos == -1) {
- return null;
- } else if (pos == nidAndNss.length() - 1) {
- return null;
- } else {
- String nssString = nidAndNss.substring(pos + 1);
- return this.isValidNSS(nssString) ? nssString : null;
- }
- }
-
- private boolean isValidNSSChar(char nssChar) {
- return nssChar == '%' || nssChar >= 'A' && nssChar <= 'Z' || nssChar >= 'a' && nssChar <= 'z' || nssChar >= '0' && nssChar <= '9' || nssChar >= '\'' && nssChar <= '.' || nssChar == '!' || nssChar == '$' || nssChar == ':' || nssChar == ';' || nssChar == '=' || nssChar == '@' || nssChar == '_';
- }
-
- private boolean isValidHexChar(char rawChar) {
- return rawChar >= 'A' && rawChar <= 'F' || rawChar >= 'a' && rawChar <= 'f' || rawChar >= '0' && rawChar <= '9';
- }
-
- private static String encodeChar(char rawChar) {
- String hexString = Integer.toHexString(rawChar);
- hexString = hexString.toUpperCase();
- return hexString.length() == 1 ? "%0" + hexString : '%' + hexString;
- }
-
- private boolean isValidNID(String nidString) throws MalformedURNException {
- if (nidString == null) {
- return false;
- } else {
- int nidLength = nidString.length();
- if (nidLength < 1) {
- return false;
- } else {
- for(int pos = 0; pos < nidLength; ++pos) {
- char testChar = nidString.charAt(pos);
- if (!this.isValidNIDChar(testChar)) {
- return false;
- }
-
- if (testChar == '%') {
- if (pos + 2 >= nidLength || !this.isValidHexChar(nidString.charAt(pos + 1)) || !this.isValidHexChar(nidString.charAt(pos + 2))) {
- return false;
- }
- } else if (this.isReservedChar(testChar)) {
- throw new MalformedURNException("unescaped reserved character '" + testChar + "' used.");
- }
- }
-
- return true;
- }
- }
- }
-
- public boolean sameNID(URN otherURN) {
- String thisNID = this.nid.toLowerCase();
- String thatNID = otherURN.getNID().toLowerCase();
- return thisNID.equals(thatNID);
- }
-
- public static String encodeString(String inString) {
- StringBuffer outStringBuffer = new StringBuffer("");
- int pos = 0;
-
- while(pos < inString.length()) {
- char testChar = inString.charAt(pos++);
- if (!isValidNSSCharStatic(testChar)) {
- outStringBuffer.append(encodeChar(testChar));
- } else {
- outStringBuffer.append(testChar);
- }
- }
-
- return new String(outStringBuffer);
- }
- }
-